import jsonpath

data = {
    "a":1,
    "b":[1,2,3]
}

def extract(resp, attr_name, exp):
    try:
         resp.json = resp.json()
    except Exception:
        resp.json = {}
    attr = getattr(resp, attr_name)#动态获取响应对象的指定属性
    '''
    例如：attr_name='json' → 获取 resp.json

例如：attr_name='text' → 获取 resp.text

例如：attr_name='headers' → 获取 resp.headers
    '''
    res = jsonpath.jsonpath(attr, exp)

    return res[0]


